package com.work.music.controller;

import com.work.music.common.ResponseJson;
import com.work.music.exception.AjaxOperationFailException;
import com.work.music.exception.ErrorCode;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

public class BaseController {

    @ExceptionHandler(AjaxOperationFailException.class)
    @ResponseBody
    public ResponseJson handleOperationFailException(NativeWebRequest req, AjaxOperationFailException e) {
        ResponseJson responseJson = new ResponseJson();
        responseJson.setStatus(ErrorCode.OPERATION_FAIL);
        responseJson.setMsg(e.getErrorMessage());
        return responseJson;
    }

    @ExceptionHandler(DataAccessException.class)
    @ResponseBody
    public ResponseJson handleDataValidationException(NativeWebRequest req, DataAccessException e) {
        ResponseJson responseJson = new ResponseJson();
        responseJson.setStatus(ErrorCode.DATABASE_OPERATION_FAIL);
        responseJson.setMsg("数据库操作失败！");
        e.printStackTrace();
        return responseJson;
    }
}
